package com.gjs.freechat.server.component.bean;

import java.util.List;

import com.gjs.freechat.utils.DateUtil;

public class Conversation {
	public static int CONVO_TYPE_WAITING = 0;
	public static int CONVO_TYPE_NORMAL = 1;
	public static int CONVO_TYPE_FRIEND = 2;
	//public static int CONVO_TYPE_TEAM = 3;
	
	private String id;
	private int type;      //0:waiting,1:normal,2:friend
	private String userId;
	private String partnerNickName;
	private String friendRequestSender;
	private UserInfo partner;
	private List<Message> messages;
	private int unreadMessages;
	private Message lastMessage;
	private String lastMsgTime;
	//private String lastMsgContent;
	private volatile boolean matching = false;
	private String createTime;
	
	
	public Conversation() {}
	
	public Conversation(String id, int type, String userId, UserInfo partner, int unreadMessages) {
		this.id = id;
		this.type = type;
		this.userId = userId;
		this.partner = partner;
		this.unreadMessages = unreadMessages;
		setPartnerNickName(partner);
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public UserInfo getPartner() {
		return partner;
	}
	public void setPartner(UserInfo partner) {
		this.partner = partner;
	}
	
	public String getPartnerId() {
		if (partner == null)
			return null;
		return partner.getId();
	}
	
	
	public String getPartnerNickName() {
		return partnerNickName;
	}

	public void setPartnerNickName(String partnerNickName) {
		this.partnerNickName = partnerNickName;
		if (partner != null)
			partner.setNickName(partnerNickName);
	}
	
	public void setPartnerNickName(UserInfo userInfo) {
		if (userInfo == null)
			return;
		String nickName = userInfo.getNickName();
		partnerNickName = (nickName != null && nickName.length() > 0) ? nickName : userInfo.getAvatarIconName();
 	}

	public int getPartnerStatus() {
		if (partner == null)
			return 0;
		return partner.getLogined();
	}

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
		if (messages == null || messages.size() == 0) {
			return;
		}
		lastMessage = messages.get(messages.size() - 1);
//		for (Message msg : messages) {
//			if (lastMessage.getMsgId() < msg.getMsgId()) {
//				lastMessage = msg;
//			}
//		}
		if (lastMessage != null) {
			lastMsgTime = lastMessage.getSendTime();
			String sendTimeYMD = DateUtil.getDate(lastMsgTime);
			String nowYMD = DateUtil.formatDate(DateUtil.getCurrentDate(), DateUtil.DATE_PATTERN_LINE);
			if (sendTimeYMD.equals(nowYMD)) {
				lastMsgTime = DateUtil.getHmTime(lastMsgTime);
			}
		}
		
	}

	public String getLastMsgTime() {
		return lastMsgTime;
	}

	public void setLastMsgTime(String lastMsgTime) {
		this.lastMsgTime = lastMsgTime;
	}

	public int getUnreadMessages() {
		return unreadMessages;
	}

	public void setUnreadMessages(int unreadMessages) {
		this.unreadMessages = unreadMessages;
	}

	public Message getLastMessage() {
		return lastMessage;
	}

	public void setLastMessage(Message lastMessage) {
		this.lastMessage = lastMessage;
	}
	
	
	public boolean isMatching() {
		return matching;
	}

	public void setMatching(boolean matching) {
		this.matching = matching;
	}
	
	

	public String getFriendRequestSender() {
		return friendRequestSender;
	}

	public void setFriendRequestSender(String friendRequestSender) {
		this.friendRequestSender = friendRequestSender;
	}
	

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	
	public String getLastEventTime() {
		String lastTime = createTime;
		if (lastMessage != null)
			lastTime = lastMessage.getRealSendTime();
		return lastTime;
	}
	

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Conversation convo = (Conversation) obj;
		if (id != null) {
			return id.equals(convo.getId());
		} else {
			return false;
		}
	}
	
}
